#============================================================
#	Controller
#============================================================
# @datetime: 2022-4-27 21:41:00
#============================================================
class_name PlayerController
extends Node2D


const Key = {
	Jump = "controller_jump",
}


# 可以跳跃的次数
const JUMP_COUNT_MAX : int = 3


onready var move_controller : SimplePlatformController = $MoveController
onready var input_controller : InputController = $InputController

onready var counter_wrapper : CounterWrapper = WrapperHelper.get_counter(owner)
onready var timer_wrapper : TimerWrapper = WrapperHelper.get_timer(owner)
onready var property_wrapper : PropertyWrapper = WrapperHelper.get_property(owner)


#============================================================
#   Set/Get
#============================================================
func get_host() -> KinematicBody2D:
	return owner as KinematicBody2D


#============================================================
#   内置
#============================================================
func _enter_tree():
	counter_wrapper = CounterWrapper.new(owner)
	timer_wrapper = TimerWrapper.new(owner)


func _ready():
	WrapperHelper.get_property(owner).connect("property_changed", self, "_property_changed")
	# 设置 移动 Controller 的属性
	move_controller.set_property_data(property_wrapper.get_property_data())


#============================================================
#   自定义
#============================================================
##  控制行为
## @vector  
func control(vector: Vector2):
	if vector:
		if vector.y < 0:
			jump()
		elif vector.y > 0:
			fall()
		move(vector.x)


##  移动 
## @left_right  
func move(left_right: float):
	move_controller.move_left_right(left_right)


##  跳跃
func jump() -> bool:
	# 不要放到末尾，因为这里有个优先级的问题，如果放到当前方法的语句块末尾
	# 则会在之前启动的 timer 之后可能会停止，造成逻辑错误
	if (get_host().is_on_floor() 
		or move_controller.is_can_jump()
	):
		counter_wrapper.clear(Key.Jump)
		timer_wrapper.stop(Key.Jump)
	
	if not timer_wrapper.is_enabled(Key.Jump):
		return false
	
	var jump_height : float = property_wrapper.get_property("jump_height")
	if counter_wrapper.get_count(Key.Jump) == 0:
		# 从地板上跳
		move_controller.jump(jump_height)
		# 跳跃次数增加 1 次
		counter_wrapper.add(Key.Jump, 1)
		# 跳跃间隔
		timer_wrapper.start(Key.Jump, 0.5)
		return true
	
	else:
		
		# 跳跃次数没有达到最大值
		if counter_wrapper.eq(Key.Jump, JUMP_COUNT_MAX) < 0:
			move_controller.jump(0.6 * jump_height, true)
			# 间隔倒计时，这个时间内这个 key 没有启动过，则返回 true 并启动倒计时
			timer_wrapper.start(Key.Jump, 0.5)
			# 计数：跳跃一次
			counter_wrapper.add(Key.Jump, 1)
			return true
	
	return false


##  落下 
onready var collision_wrapper : CollisionWrapper = WrapperHelper.get_collision(owner)
func fall():
	collision_wrapper.disabled()


#============================================================
#   连接信号
#============================================================
func _property_changed(property, value):
	if property in move_controller:
		move_controller.set(property, value)

